export default function (url, { params = {}, jsonp, fnName } = {}) {
  return new Promise((resolve, reject) => {
    // 判断fnName是否存在，如果不存在生成一个函数名
    if (!fnName) {
      // 生成函数名
      fnName = 'z' + Math.random().toString(36).slice(2)
    }
    // 将成功的回调函数作为全局window下的属性
    window[fnName] = resolve // window.aaa = resolve;

    // prod=pc&wd=三国&cb=pj1715
    // console.log(params);// {prod: 'pc', wd: '三国', cb: 'pj1715'}
    // console.log(Object.keys(params));// ["prod","wd","cb"]
    // console.log(Object.keys(params).map(key=>key+"="+params[key]));// ["prod=pc","wd=三国","cb=pj1715"]
    // console.log(Object.keys(params).map(key=>key+"="+params[key]).join("&"));// prod=pc&wd=三国&cb=pj1715
    if (jsonp) params[jsonp] = fnName // {cb:}
    if (params)
      url +=
        '?' +
        Object.keys(params)
          .map((key) => key + '=' + params[key])
          .join('&')
    const script = document.createElement('script')
    script.onload = function () {
      document.body.removeChild(script)
      delete window[fnName]
    }
    script.src = url
    document.body.appendChild(script)
  })
}
